﻿using System;
using System.Collections.Generic;

namespace SwyishChess.Domain.Piece
{
    public class Bishop : ChessPiece, IDiagonallyMovable
    {
        /// <summary>
        /// Maximum movable range not including the current locatino of the chess piece 
        /// </summary>
        public const int MAXIMUM_MOVABLE_RANGE = 8;

        /// <summary>
        /// Overloaded constructor 
        /// </summary>
        /// <param name="row">Row location</param>
        /// <param name="column">Column location</param>
        /// <param name="color">Color of the chess piece</param>
        public Bishop(int owningPlayerId)
        {
            OwningPlayerId = owningPlayerId;
        }

        /// <summary>
        /// Get all possible main diagonal moves (top left to bottom right)
        /// </summary>
        /// <returns>Coordinate of possible main diagonal moves in integer array</returns>
        public int[,] GetPossibleMainDiagonalMoves()
        {
            int possibleRowAndColumns = MAXIMUM_MOVABLE_RANGE * -1;
            int[,] possibleMainDiagonalMoves = new int[MAXIMUM_MOVABLE_RANGE * 2, 2];

            for (int row = 0; row < (MAXIMUM_MOVABLE_RANGE * 2); row++)
            {
                possibleMainDiagonalMoves[row, 0] = possibleRowAndColumns;
                possibleMainDiagonalMoves[row, 1] = possibleRowAndColumns;

                possibleRowAndColumns++;
                if (possibleRowAndColumns == 0)
                    possibleRowAndColumns++;
            }

            return possibleMainDiagonalMoves;
        }

        /// <summary>
        /// Get all possible main diagonal moves (top right to bottom left)
        /// </summary>
        /// <returns>Coordinate of possible minor diagonal moves in integer array</returns>
        public int[,] GetPossibleMinorDiagonalMoves()
        {
            int possibleRowAndColumns = MAXIMUM_MOVABLE_RANGE * -1;
            int[,] possibleMinorDiagonalMoves = new int[MAXIMUM_MOVABLE_RANGE * 2, 2];

            for (int row = 0; row < (MAXIMUM_MOVABLE_RANGE * 2); row++)
            {
                if (row < 8)
                {
                    possibleMinorDiagonalMoves[row, 0] = possibleRowAndColumns;
                    possibleMinorDiagonalMoves[row, 1] = possibleRowAndColumns * -1;
                }
                else
                {
                    possibleMinorDiagonalMoves[row, 0] = possibleRowAndColumns * -1;
                    possibleMinorDiagonalMoves[row, 1] = possibleRowAndColumns;
                }

                if (row < 8)
                {
                    possibleRowAndColumns++;
                    if (possibleRowAndColumns == 0)
                        possibleRowAndColumns++;
                }
                else
                {
                    possibleRowAndColumns--;
                    if (possibleRowAndColumns == 0)
                        possibleRowAndColumns--;
                }

            }
            return possibleMinorDiagonalMoves;
        }

        /// <summary>
        /// All possible moves by the chess piece
        /// </summary>
        public override List<int[,]> AllPossibleMoves
        {
            get
            {
                List<int[,]> possibleMoves = new List<int[,]>();
                possibleMoves.Add(GetPossibleMainDiagonalMoves());
                possibleMoves.Add(GetPossibleMinorDiagonalMoves());
                return possibleMoves;
            }
        }

        public override string ToString()
        {
            return "Bishop";
        }
    }
}
